home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / boekhoud / finan / BADGER finance v1.0 beta 2.exe / xampplite / phpMyAdmin / libraries / plugin_interface.lib.php < prev    next >
PHP Script  |  2006-01-17  |  10KB  |  288 lines

  1. <?php
  2. /* $Id: plugin_interface.lib.php,v 1.3 2006/01/17 17:02:30 cybot_tm Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Generic plugin interface.
  7.  */
  8.  
  9. /**
  10.  * array PMA_getPlugins(string $plugins_dir, mixed $plugin_param)
  11.  *
  12.  * Reads all plugin information from directory $plugins_dir.
  13.  *
  14.  * @uses    ksort()
  15.  * @uses    opendir()
  16.  * @uses    readdir()
  17.  * @uses    is_file()
  18.  * @uses    eregi()
  19.  * @param   string  $plugins_dir    directrory with plugins
  20.  * @param   mixed   $plugin_param   parameter to plugin by which they can decide whether they can work
  21.  * @return  array                   list of plugins
  22.  */
  23. function PMA_getPlugins($plugins_dir, $plugin_param)
  24. {
  25.     /* Scan for plugins */
  26.     $plugin_list = array();
  27.     if ($handle = @opendir($plugins_dir)) {
  28.         $is_first = 0;
  29.         while ($file = @readdir($handle)) {
  30.             if (is_file($plugins_dir . $file) && eregi('\.php$', $file)) {
  31.                 include $plugins_dir . $file;
  32.             }
  33.         }
  34.     }
  35.     ksort($plugin_list);
  36.     return $plugin_list;
  37. }
  38.  
  39. /**
  40.  * string PMA_getString(string $name)
  41.  *
  42.  * returns locale string for $name or $name if no locale is found
  43.  *
  44.  * @uses    $GLOBALS
  45.  * @param   string  $name   for local string
  46.  * @return  string          locale string for $name
  47.  */
  48. function PMA_getString($name)
  49. {
  50.     return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
  51. }
  52.  
  53. /**
  54.  * string PMA_pluginCheckboxCheck(string $section, string $opt)
  55.  *
  56.  * returns html input tag option 'checked' if plugin $opt should be set by config or request
  57.  *
  58.  * @uses    $_REQUEST
  59.  * @uses    $GLOBALS['cfg']
  60.  * @uses    $GLOBALS['timeout_passed']
  61.  * @param   string  $section    name of config section in
  62.  *                              $GLOBALS['cfg'][$section] for plugin
  63.  * @param   string  $opt        name of option
  64.  * @return  string              hmtl input tag option 'checked'
  65.  */
  66. function PMA_pluginCheckboxCheck($section, $opt)
  67. {
  68.     if ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
  69.         (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt])) {
  70.         return ' checked="checked"';
  71.     }
  72.     return '';
  73. }
  74.  
  75. /**
  76.  * string PMA_pluginGetDefault(string $section, string $opt)
  77.  *
  78.  * returns default value for option $opt
  79.  *
  80.  * @uses    htmlspecialchars()
  81.  * @uses    $_REQUEST
  82.  * @uses    $GLOBALS['cfg']
  83.  * @uses    $GLOBALS['timeout_passed']
  84.  * @param   string  $section    name of config section in
  85.  *                              $GLOBALS['cfg'][$section] for plugin
  86.  * @param   string  $opt        name of option
  87.  * @return  string              default value for option $opt
  88.  */
  89. function PMA_pluginGetDefault($section, $opt)
  90. {
  91.     if (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
  92.         return htmlspecialchars($_REQUEST[$opt]);
  93.     } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
  94.         return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
  95.     }
  96.     return '';
  97. }
  98.  
  99. /**
  100.  * string PMA_pluginIsActive(string $section, string $opt, string $val)
  101.  *
  102.  * returns html input tag option 'checked' if option $opt should be set by config or request
  103.  *
  104.  * @uses    $_REQUEST
  105.  * @uses    $GLOBALS['cfg']
  106.  * @uses    $GLOBALS['timeout_passed']
  107.  * @param   string  $section    name of config section in
  108.  *                              $GLOBALS['cfg'][$section] for plugin
  109.  * @param   string  $opt        name of option
  110.  * @param   string  $val        value of option to check against
  111.  * @return  string              html input tag option 'checked'
  112.  */
  113. function PMA_pluginIsActive($section, $opt, $val)
  114. {
  115.     if ( ! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
  116.         if ($_REQUEST[$opt] == $val) {
  117.             return ' checked="checked"';
  118.         }
  119.     } elseif (isset($GLOBALS['cfg'][$section][$opt]) &&  $GLOBALS['cfg'][$section][$opt] == $val) {
  120.         return ' checked="checked"';
  121.     }
  122.     return '';
  123. }
  124.  
  125. /**
  126.  * string PMA_pluginGetChoice(string $section, string $name, array &$list)
  127.  *
  128.  * returns html radio form element for plugin choice
  129.  *
  130.  * @uses    PMA_pluginIsActive()
  131.  * @uses    PMA_getString()
  132.  * @param   string  $section    name of config section in
  133.  *                              $GLOBALS['cfg'][$section] for plugin
  134.  * @param   string  $name       name of radio element
  135.  * @param   array   &$list      array with plugin configuration defined in plugin file
  136.  * @return  string              html input radio tag
  137.  */
  138. function PMA_pluginGetChoice($section, $name, &$list)
  139. {
  140.     $ret = '';
  141.     foreach ($list as $plugin_name => $val) {
  142.         $ret .= '<!-- ' . $plugin_name . ' -->' . "\n";
  143.         $ret .= '<input type="radio" name="' . $name . '" value="' . $plugin_name . '"'
  144.             . ' id="radio_plugin_' . $plugin_name . '"'
  145.             . ' onclick="if(this.checked) { hide_them_all();'
  146.                     .' document.getElementById(\'' . $plugin_name . '_options\').style.display = \'block\'; };'
  147.                 .' return true"'
  148.             . PMA_pluginIsActive($section, $name, $plugin_name) . '/>' . "\n";
  149.         $ret .= '<label for="radio_plugin_' . $plugin_name . '">'
  150.             . PMA_getString($val['text']) . '</label>' . "\n";
  151.         $ret .= '<br /><br />' . "\n";
  152.     }
  153.     return $ret;
  154. }
  155.  
  156. /**
  157.  * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
  158.  *
  159.  * returns single option in a table row
  160.  *
  161.  * @uses    PMA_getString()
  162.  * @uses    PMA_pluginCheckboxCheck()
  163.  * @uses    PMA_pluginGetDefault()
  164.  * @param   string  $section        name of config section in
  165.  *                                  $GLOBALS['cfg'][$section] for plugin
  166.  * @param   string  $plugin_name    unique plugin name
  167.  * @param   string  $id             option id
  168.  * @param   array   &$opt           plugin option details
  169.  * @return  string                  table row with option
  170.  */
  171. function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
  172. {
  173.     $ret = '';
  174.     $ret .= '<tr>';
  175.     if ($opt['type'] == 'bool') {
  176.         $ret .= '<td colspan="2">';
  177.         $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
  178.             . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
  179.             . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']) .' />';
  180.         $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
  181.             . PMA_getString($opt['text']) . '</label>';
  182.         $ret .= '</td>';
  183.     } elseif ($opt['type'] == 'text') {
  184.         $ret .= '<td>';
  185.         $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '">'
  186.             . PMA_getString($opt['text']) . '</label>';
  187.         $ret .= '</td><td>';
  188.         $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
  189.             . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
  190.             . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
  191.             . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '' )
  192.             . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '' ) . ' />';
  193.         $ret .= '</td>';
  194.     } else {
  195.         /* This should be seen only by plugin writers, so I do not thing this
  196.          * needs translation. */
  197.         $ret .= '<td colspan="2">';
  198.         $ret .= 'UNKNOWN OPTION IN IMPORT PLUGIN ' . $plugin_name . '!';
  199.         $ret .= '</td>';
  200.     }
  201.     $ret .= '</tr>';
  202.     return $ret;
  203. }
  204.  
  205. /**
  206.  * string PMA_pluginGetOptions(string $section, array &$list)
  207.  *
  208.  * return html fieldset with editable options for plugin
  209.  *
  210.  * @uses    PMA_getString()
  211.  * @uses    PMA_pluginGetOneOption()
  212.  * @param   string  $section    name of config section in $GLOBALS['cfg'][$section]
  213.  * @param   array   &$list      array with plugin configuration defined in plugin file
  214.  * @return  string              html fieldset with plugin options
  215.  */
  216. function PMA_pluginGetOptions($section, &$list)
  217. {
  218.     $ret = '';
  219.     // Options for plugins that support them
  220.     foreach ($list as $plugin_name => $val) {
  221.         $ret .= '<fieldset id="' . $plugin_name . '_options" class="options">';
  222.         $ret .= '<legend>' . PMA_getString($val['options_text']) . '</legend>';
  223.         if (isset($val['options'])) {
  224.             $ret .= '<table class="form">';
  225.             $ret .= '<tbody>';
  226.             foreach ($val['options'] as $id => $opt) {
  227.                 $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
  228.             }
  229.             $ret .= '</tbody>';
  230.             $ret .= '</table>';
  231.         } else {
  232.             $ret .= $GLOBALS['strNoOptions'];
  233.         }
  234.         $ret .= '</fieldset>';
  235.     }
  236.     return $ret;
  237. }
  238.  
  239. function PMA_pluginGetJavascript(&$list) {
  240.     $ret = '
  241.     <script type="text/javascript" language="javascript">
  242.     //<![CDATA[
  243.     function hide_them_all() {
  244.         ';
  245.     foreach ($list as $plugin_name => $val) {
  246.         $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "none";' . "\n";
  247.     }
  248.     $ret .= '
  249.     }
  250.  
  251.     function init_options() {
  252.         hide_them_all();
  253.         ';
  254.     foreach ($list as $plugin_name => $val) {
  255.         $ret .= 'if (document.getElementById("radio_plugin_' . $plugin_name . '").checked) {' . "\n";
  256.         $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "block";' . "\n";
  257.         $ret .= ' } else ' . "\n";
  258.     }
  259.     $ret .= '
  260.         {
  261.             ;
  262.         }
  263.     }
  264.  
  265.     function match_file(fname) {
  266.         farr = fname.toLowerCase().split(".");
  267.         if (farr.length != 0) {
  268.             len = farr.length
  269.             if (farr[len - 1] == "gz" || farr[len - 1] == "bz2" || farr[len -1] == "zip") len--;
  270.             switch (farr[len - 1]) {
  271.                 ';
  272.     foreach ($list as $plugin_name => $val) {
  273.         $ret .= 'case "' . $val['extension'] . '" :';
  274.         $ret .= 'document.getElementById("radio_plugin_' . $plugin_name . '").checked = true;';
  275.         $ret .= 'init_options();';
  276.         $ret .= 'break;' . "\n";
  277.     }
  278.     $ret .='
  279.             }
  280.         }
  281.     }
  282.     //]]>
  283.     </script>
  284.     ';
  285.     return $ret;
  286. }
  287. ?>
  288.